#include "stdafx.h"
#include "wf_modifybevel.h"

#include "pf_snapper.h"
#include "pf_information.h"


WF_ModifyBevel::WF_ModifyBevel(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Bevel Entities",
                           container, graphicView) {

    entity1 = NULL;
    coord1 = PF_Vector(false);
    entity2 = NULL;
    coord2 = PF_Vector(false);
}


QAction* WF_ModifyBevel::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Bevel")
    QAction* action = new QAction(tr("&Bevel"),  NULL);
	action->setIcon(QIcon(":/extui/modifybevel.png"));
    //action->zetStatusTip(tr("Bevel Entities"));
    return action;
}


void WF_ModifyBevel::init(int status) {
    PF_ActionInterface::init(status);

    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;
}



void WF_ModifyBevel::trigger() {

    PF_DEBUG->print("WF_ModifyBevel::trigger()");

    if (entity1!=NULL && entity1->isAtomic() &&
            entity2!=NULL && entity2->isAtomic()) {

        PF_Modification m(*container, graphicView);
        m.bevel(coord1, (PF_AtomicEntity*)entity1,
                coord2, (PF_AtomicEntity*)entity2,
                data);

        entity1 = NULL;
        entity2 = NULL;
        setStatus(SetEntity1);

        PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
    }
}



void WF_ModifyBevel::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("WF_ModifyBevel::mouseMoveEvent begin");

    PF_Vector mouse = graphicView->toGraph(e->x(), e->y());
    PF_Entity* se = catchEntity(e, LX_Define::ResolveAll);

    switch (getStatus()) {
    case SetEntity1:
        coord1 = mouse;
        entity1 = se;
        break;

    case SetEntity2:
		if (entity1!=NULL && PF_Information::isTrimmable(entity1)) {
        	coord2 = mouse;
	        entity2 = se;
		}
        break;

    default:
        break;
    }

    PF_DEBUG->print("WF_ModifyBevel::mouseMoveEvent end");
}



void WF_ModifyBevel::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case SetEntity1:
            if (entity1!=NULL && entity1->isAtomic()) {
                setStatus(SetEntity2);
            }
            break;

        case SetEntity2:
            if (entity2!=NULL && entity2->isAtomic() &&
			    PF_Information::isTrimmable(entity1, entity2)) {
                trigger();
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_ModifyBevel::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    switch (getStatus()) {
    case SetEntity1:
    case SetEntity2:
        if (checkCommand("length1", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetLength1);
        } else if (checkCommand("length2", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetLength2);
        } else if (checkCommand("trim", c)) {
            //deletePreview();
            //lastStatus = (Status)getStatus();
            //setStatus(SetTrim);
            data.trim = !data.trim;
            PF_DIALOGFACTORY->requestOptions(this, true, true);
        }
        break;

    case SetLength1: {
            bool ok;
            double l = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.length1 = l;
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;

    case SetLength2: {
            bool ok;
            double l = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.length2 = l;
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;

        /*case SetTrim: {
        if (checkCommand()) {
        data.trim = true;
    } else if (c==cmdNo.lower() || c==cmdNo2) {
        data.trim = false;
                } else {
                    PF_DIALOGFACTORY->commandMessage(tr("Please enter 'Yes' "
               "or 'No'"));
                }
                PF_DIALOGFACTORY->requestOptions(this, true, true);
                setStatus(lastStatus);
            }
            break;*/

    default:
        break;
    }
}



PF_StringList WF_ModifyBevel::getAvailableCommands() {
    PF_StringList cmd;
    switch (getStatus()) {
    case SetEntity1:
    case SetEntity2:
        cmd += command("length1");
        cmd += command("length2");
        cmd += command("trim");
        break;
    default:
        break;
    }
    return cmd;
}



void WF_ModifyBevel::showOptions() {
    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
}



void WF_ModifyBevel::hideOptions() {
    PF_ActionInterface::hideOptions();

    PF_DIALOGFACTORY->requestOptions(this, false);
}



void WF_ModifyBevel::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetEntity1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Select first entity"),
                                            tr("Cancel"));
        break;
    case SetEntity2:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Select second entity"),
                                            tr("Back"));
        break;
    case SetLength1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter length 1:"),
                                            tr("Back"));
        break;
    case SetLength2:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter length 2:"),
                                            tr("Back"));
        break;
        /*case SetTrim:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Trim on? (yes/no):"),
                                                "");
            break;*/
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyBevel::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyBevel::updateToolBar() {
    PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
}


// EOF
